﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class NguoiDung : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy cs = new clsXuLy();
    string frmName = "frmNguoiDung";
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Title = "Quản lí người dùng | " + Header;
            btnCancel_Click(sender, e);
        }
    }
    public void LoadNhanVien()
    {
        drNhanVien.Items.Clear();
        drNhanVien.DataSource = cs.GetData("sp_NguoiDung_Load");
        drNhanVien.DataBind();
        drNhanVien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        drNhanVien.SelectedIndex = 0;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        LoadNhanVien();
        LoadNhomNguoiDung();
        GV.DataSource = cs.GetData("sp_List_TaiKhoan");
        GV.DataBind();
        txttaikhoan.Text = "";
        txttaikhoan.Focus();
        txtmatkhau.Text = "";
        txtnhaplaimk.Text = "";
        ckActive.Checked = true;
        btnSave.Visible = true;
        btncapnhat.Visible = false;
        txtmatkhau.ReadOnly = false;
        txtnhaplaimk.ReadOnly = false;
        txttaikhoan.ReadOnly = false;
        drNhanVien.Enabled = true;
    }
    public void LoadNhomNguoiDung()
    {
        drNhomnguoidung.Items.Clear();
        drNhomnguoidung.DataSource = cs.GetData("sp_NhomNguoiDung_Load");
        drNhomnguoidung.DataBind();
        drNhomnguoidung.Items.Insert(0, new ListItem("--Chọn nhóm người dùng--", ""));
        drNhomnguoidung.SelectedIndex = 0;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        p = new SqlParameter[6];
        if (txtmatkhau.Text.Trim().Equals(txtnhaplaimk.Text.Trim()))
        {
            try
            {
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@MaNV", GetGuidDrop(drNhanVien));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@User", txttaikhoan.Text.Trim());
                p[2].SqlDbType = SqlDbType.NVarChar;
                p[3] = new SqlParameter("@Pass", cs.Encrypt(txtmatkhau.Text.Trim()));
                p[3].SqlDbType = SqlDbType.NVarChar;
                p[4] = new SqlParameter("@NhomNguoiDung", drNhomnguoidung.SelectedValue);
                p[4].SqlDbType = SqlDbType.Int;
                p[5] = new SqlParameter("@SuDung", ckActive.Checked ? 1 : 0);
                p[5].SqlDbType = SqlDbType.Int;
                cs.ExecuteNonQuerry("sp_VntNguoiDungInsert", p);
                gstGetMess("Thêm tài khoản thành công", "");
                btnCancel_Click(sender, e);
            }
            catch (Exception ex)
            {
                gstGetMess(ex.Message, "");
            }
        }
        else gstGetMess("Mật khẩu chưa trùng nhau", "");

    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    public object GetGuidDrop(DropDownList dl)
    {
        try
        {
            if (dl.SelectedIndex == 0) return DBNull.Value;
            else
            {
                Guid ID = new Guid(dl.SelectedValue);
                return ID;
            }
        }
        catch
        {
            return DBNull.Value;
        }
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@ID", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = cs.GetData("sp_List_TaiKhoan_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    drNhanVien.Items.Clear();
                    drNhanVien.DataSource = cs.GetData("sp_vnttblnhansu_List");
                    drNhanVien.DataBind();
                    txttaikhoan.Text = dt.Rows[0]["username"].ToString();
                    
                    drNhomnguoidung.SelectedValue = dt.Rows[0]["manhomnguoidung"].ToString();
                    drNhanVien.SelectedValue = dt.Rows[0]["manhansu"].ToString();
                    txttaikhoan.ReadOnly = true;
                    txtmatkhau.ReadOnly = true;
                    txtnhaplaimk.ReadOnly = true;
                    drNhanVien.Enabled = false;

                    if (dt.Rows[0]["trangthai"].ToString() == "1")
                        ckActive.Checked = true;
                    else
                        ckActive.Checked = false;

                }
                catch (Exception ex)
                {
                    gstGetMess(ex.Message, "");
                }

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {

                try
                {
                    cs.ExecuteNonQuerry("sp_Nguoidung_Del", p);
                    btnCancel_Click(sender, e);
                    gstGetMess("Xóa thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Lỗi: " + ax.Message, "");

                }

            }
    }

    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        try
        {
            Guid gID = new Guid(hdID.Value);
            p = new SqlParameter[4];

            p[0] = new SqlParameter("@User", txttaikhoan.Text.Trim());
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@Pass", cs.Encrypt(txtmatkhau.Text.Trim()));
            p[1].SqlDbType = SqlDbType.NVarChar;
            p[2] = new SqlParameter("@NhomNguoiDung", drNhomnguoidung.SelectedValue);
            p[2].SqlDbType = SqlDbType.Int;
            p[3] = new SqlParameter("@SuDung", ckActive.Checked ? 1 : 0);
            p[3].SqlDbType = SqlDbType.Int;

            cs.ExecuteNonQuerry("sp_VntNguoiDungUpdate", p);
            btnCancel_Click(sender, e);
            gstGetMess("Cập nhật thành công", "");
        }
        catch (Exception ax)
        {

            gstGetMess("Lỗi: " + ax.Message, "");
        }
    }
}